package com.abkj.platform.tail.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.abkj.platform.base.dao.warp.MapperWrap;
import com.abkj.platform.base.dao.warp.SQLType;
import com.abkj.platform.tail.service.ISMSService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

@Service("SMSService")
public class SMSService extends BaseServiceImpl implements ISMSService {
	
	@Autowired
	private MapperWrap mapperWrap;

	/**
	 * 向手机发送信息
	 * 
	 * @param phoneNo，多个电话号码用“,”隔开
	 * @param name
	 * @param msg
	 * @author ServerZhang
	 * @date 2016年6月12日
	 */
	@Override
	public void sendMsg(String phoneNo, String name, String msg) {
		/*if (phoneNo == null) {
			return;
		}*/
		Map<String, Object> map = this.getForm();
		if (map.size() < 0) {
			return;
		}
		AlibabaAliqinFcSmsNumSendResponse rsp;
		TaobaoClient client = null;
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		try {
			// TODO 调用第三方服务发送手机验证码
			String url = map.get("url").toString();
			String appkey = map.get("appkey").toString();
			String secret = map.get("secret").toString();
			Object signNameObj = map.get("signName");
			String signName = "大鱼测试";
			client = new DefaultTaobaoClient(url, appkey, secret);
			req.setExtend("123456");
			req.setSmsType("normal");
			if (signNameObj != null) {
				signName = signNameObj.toString();
			}
			req.setSmsFreeSignName(signName);
			if (StringUtils.isEmpty(name)) {
				name = map.get("name").toString();
			}
			if (StringUtils.isEmpty(msg)) {
				msg = map.get("msg").toString();
			}
			req.setSmsParamString("{\"name\":\"" + name + "\",\"info\":\"" + msg
					+ "\"}");
			if (StringUtils.isEmpty(phoneNo)) {
				phoneNo = map.get("phoneNo").toString();
			}
			Pattern compile = Pattern.compile("[,，]");
			String[] split = compile.split(phoneNo);
			//String[] split = phoneNo.split("|");
			for(String no : split){
				req.setRecNum(no);
				req.setSmsTemplateCode(map.get("templateCode").toString());
				
				rsp = client.execute(req);
			}
		} catch (ApiException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
	@Override
	public Map<String, Object> getForm(){
		String sql = "SELECT * FROM smsSetting order by id desc";
		List list = this.mapperWrap.findBySql(sql);
		if (list.size() > 0) {
			return (Map<String, Object>)list.get(0);
		}else {
			return new HashMap<String, Object>();
		}
	}
	@Override
	public Boolean update(Map<String, Object> map){
		Boolean isSucess = true;
		StringBuffer sqlBuffer = new StringBuffer();
		Object phoneNoObj = map.get("phoneNo");
		if (phoneNoObj != null) {
			if (sqlBuffer.length() > 0) {
				sqlBuffer.append(",");
			}
			sqlBuffer.append("phoneNo='").append(phoneNoObj.toString()).append("'");
		}
		Object nameObj = map.get("name");
		if (nameObj != null) {
			if (sqlBuffer.length() > 0) {
				sqlBuffer.append(",");
			}
			sqlBuffer.append("name='").append(nameObj.toString()).append("'");
		}
		Object msgObj = map.get("msg");
		if (msgObj != null) {
			if (sqlBuffer.length() > 0) {
				sqlBuffer.append(",");
			}
			sqlBuffer.append("msg='").append(msgObj.toString()).append("'");
		}
		Object ableObj = map.get("able");
		if (ableObj != null) {
			if (sqlBuffer.length() > 0) {
				sqlBuffer.append(",");
			}
			sqlBuffer.append("able=").append(ableObj.toString());
		}
		Object autoObj = map.get("auto");
		if (autoObj != null) {
			if (sqlBuffer.length() > 0) {
				sqlBuffer.append(",");
			}
			sqlBuffer.append("auto=").append(autoObj.toString());
		}
		try {
			this.excuteBySql(SQLType.UPDATE, "update smssetting set " + sqlBuffer.toString());
		} catch (Exception e) {
			isSucess = false;
		}
		return isSucess;
	}
}
